package com.weixin.program.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.*;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年02⽉26⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
public class HolidayUtil {

    public static List<String> HOLIDAYS = new ArrayList<>();

    static {
        HOLIDAYS.addAll(notWorkingDays());
    }

    /**
     * 获取节假日
     *
     * @return
     */
    public static List<String> notWorkingDays() {
        List<String> holidays = new ArrayList<>();
        try {
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            String url = "http://tool.bitefu.net/jiari?d=" + year;
            String response = HttpClientProxy.get(url, "");
            log.info(response);
            JSONObject jsonObject = JSONObject.parseObject(response);
            JSONObject object = (JSONObject) jsonObject.get(year);
            Map map = object.toJavaObject(Map.class);
            Iterator iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                String next = (String) iterator.next();
                holidays.add(year + next);
            }
//            Collections.sort(holidays);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return holidays;
    }

    /**
     * 获取上午、下午
     *
     * @return
     */
    public static String getAmPm() {
        GregorianCalendar ca = new GregorianCalendar();
        int num = ca.get(GregorianCalendar.AM_PM);
        String str = "上午";
        if (num == 1) {
            str = "下午";
        }
        return str;
    }
}
